﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using quanlynhasach.Linksql;
namespace quanlynhasach
{
    public partial class frmHanghoa : Form
    {
        static HangHoa hh = new HangHoa();
        static Connection connect = new Connection();
        static HangHoasql hsql = new HangHoasql();
        DataTable dtSach = new DataTable();
        public frmHanghoa()
        {
            InitializeComponent();
            
        }

        private void LoadData()
        {
            grdtSach.DataSource = hsql.GetHangHoa();
        }

        private void AddComboBox()
        {
            comboBoxTheLoai.ResetText();
            comboBoxNCC.ResetText();
            DataTable dt = hsql.GetHangHoa("Select TenTL From TheLoai");
            int Count; 
              Count  = dt.Rows.Count;
            for (int i = 0; i < Count; i++)
            {
                string stringcomboBox = dt.Rows[i]["TenTL"].ToString();
                if (stringcomboBox != "N/A")
                    comboBoxTheLoai.Items.Add(stringcomboBox);
            }
            DataTable dt1 = hsql.GetHangHoa("Select TenNCC From NhaCungCap");
            Count = dt1.Rows.Count;
            for (int i = 0; i < Count; i++)
            {
                string stringcomboBox = dt1.Rows[i]["TenNCC"].ToString();
                if (stringcomboBox != "N/A")
                    comboBoxNCC.Items.Add(stringcomboBox);
            }
        }

        private HangHoa GetData()
        {
            HangHoa hh = new HangHoa();
            hh.MaHangHoa = masach.Text;
            hh.TenHangHoa = textBoxTenSach.Text;
            hh.GiaBan = textBoxGiaBan.Text;
            hh.GiaMua= textBoxGiaMua.Text;
            hh.SoLuong = int.Parse(textBoxSoLuong.Text);
            hh.DonVi = textboxDV.Text;
            hh.MaNCC = comboBoxNCC.Text;
            hh.TenTG = textBoxtacgia.Text;
            hh.MaTL = comboBoxTheLoai.Text;
            hh.Thue = textBoxThue.Text;
            return hh;
        }

        private void Reset()
        {
            textBoxTenSach.Text = "";
            textBoxGiaMua.Text = "0";
            textBoxGiaBan.Text = "0";
            textBoxSoLuong.Text = "0";
            textboxDV.Text = "";
            comboBoxNCC.Text = "";
            textBoxtacgia.Text = "";
            comboBoxTheLoai.Text = "";
            textBoxThue.Text = "0";
        }
        private void Enable(bool a)

        {
            grdtSach.Enabled = !a;
            btnThemSach.Enabled = !a;
            btnSuaSach.Enabled = !a;
            btnXoaSach.Enabled = !a;
            btnLuuSach.Enabled = a;
            btnHuySach.Enabled = a;
            textBoxTenSach.Enabled = a;
            textBoxGiaMua.Enabled = a;
            textBoxGiaBan.Enabled = a;
            textBoxSoLuong.Enabled = a;
            textboxDV.Enabled = a;
            comboBoxNCC.Enabled = a;
            textBoxtacgia.Enabled = a;
            comboBoxTheLoai.Enabled = a;
            textBoxThue.Enabled = a;
        }
        private void frmHanghoa_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
            LoadData();
            grdtSach.AllowUserToAddRows = false;
            AddComboBox();
        }

        private void grdtSach_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int RowIndex = e.RowIndex;
            masach.Text = grdtSach.Rows[RowIndex].Cells["MaSach"].Value.ToString();
            textBoxTenSach.Text = grdtSach.Rows[RowIndex].Cells["TenSach"].Value.ToString();
            textBoxGiaBan.Text = grdtSach.Rows[RowIndex].Cells["GiaBan"].Value.ToString();
            textBoxGiaMua.Text = grdtSach.Rows[RowIndex].Cells["GiaMua"].Value.ToString();
            comboBoxNCC.Text = grdtSach.Rows[RowIndex].Cells["TenNCC"].Value.ToString();
            if (comboBoxNCC.Text == "N/A")
                comboBoxNCC.Text = "";
            textBoxSoLuong.Text = grdtSach.Rows[RowIndex].Cells["SoLuong"].Value.ToString();
            textboxDV.Text = grdtSach.Rows[RowIndex].Cells["DonVi"].Value.ToString();
            textBoxtacgia.Text = grdtSach.Rows[RowIndex].Cells["TenTG"].Value.ToString();
            comboBoxTheLoai.Text = grdtSach.Rows[RowIndex].Cells["TenTL"].Value.ToString();
            textBoxThue.Text = grdtSach.Rows[RowIndex].Cells["Thue"].Value.ToString();
        }

        private void btnXoaSach_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Có Chắc Muốn Xóa Sách Có Mã Là " + masach.Text + " Không ?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (hsql.XoaHangHoa(masach.Text))
                {
                    MessageBox.Show("Xóa Thành Công!");
                }
            }
            LoadData();
        }

        private void btnThemSach_Click(object sender, EventArgs e)
        {
            Reset();
            Enable(true);
            masach.Text = hsql.SearchDaTaGrid();
        }

        private void btnSuaSach_Click(object sender, EventArgs e)
        {
            Enable(true); 
        }
        private void btnLuuSach_Click(object sender, EventArgs e)
        {
             HangHoa hh = GetData();
             try
             {
                 if (hsql.SuaHangHoa(hh))
                     MessageBox.Show("Cập nhật thành công");
                 else
                     if (hsql.ThemHangHoa(hh))
                         MessageBox.Show("Thêm thành công !");
                     else
                         MessageBox.Show("Xem lại !");
             }
             catch
             {
                 MessageBox.Show("Xem Lai Du Lieu Da Nhap");
             }
            LoadData();
            Enable(false);
        }

        private void btnHuySach_Click(object sender, EventArgs e)
        {
            Enable(false);
        }

        private void textBoxSoLuong_TextChanged(object sender, EventArgs e)
        {
            int i;
            if (!int.TryParse(textBoxSoLuong.Text, out i))
            {
                if (textBoxSoLuong.Text != "")
                {
                    MessageBox.Show("Số Lượng Phải Là Số Nguyên lớn hơn 0");
                    textBoxSoLuong.Text = "";
                }
                return;
            }
        }        
    }
}
